//ASCII, Hex, Unicode, Base64, Binary, Octal Converter (Javascript) is Copyright 2013 TextMechanic.com All Rights Reserved
function selbox(hexdiv, unidiv, base64div, bindiv, octdiv, btnval, func) {
        document.getElementById('hexdivbtn').style.display = hexdiv;
        document.getElementById('hex').style.display = hexdiv;
        document.getElementById('unidivbtn').style.display = unidiv;
        document.getElementById('unicode').style.display = unidiv;
        document.getElementById('base64divbtn').style.display = base64div;
        document.getElementById('base64').style.display = base64div;
        document.getElementById('bindivbtn').style.display = bindiv;
        document.getElementById('binary').style.display = bindiv;
        document.getElementById('octdivbtn').style.display = octdiv;
        document.getElementById('octal').style.display = octdiv;
        document.getElementById('asciibtn').value = btnval;
        document.getElementById('asciibtn').setAttribute('onclick', func);
        document.getElementById('ascii').setAttribute('onkeyup', 'if(document.getElementById(\'c_onkeystroke\').checked==true){' + func + '}');
}



function pad(num, padnum) {
        var padlen = padnum - num.toString().length;
        var padding = '';
        for (var x = 0; x < padlen; x++) {
                padding = '0' + padding;
        }
        num = padding + num;
        return num;
}
String.prototype.trim = function () {
        return this.replace(/^\s+|\s+$/g, "");
}


function ascii2hex() {
        var text = document.getElementById('ascii').value;
        var delimiterleft = document.getElementById('hexdelimiterleft').value;
        var delimiterright = document.getElementById('hexdelimiterright').value;
        text = text.replace(/\r/gi, '');
        text = text.split('\n');
        var textlen = text.length;
        var textout = new Array();
        var textlinearr = new Array();
        for (var x = 0; x < textlen; x++) {
                textlinearr = new Array();
                textline = text[x];
                textlinelen = textline.length;
                for (var y = 0; y < textlinelen; y++) {
                        textlinearr[y] = delimiterleft + textline.charCodeAt(y).toString(16) + delimiterright;
                }
                textout[x] = textlinearr.join('');
        }
        textout = textout.join(delimiterleft + '0a' + delimiterright);
        document.getElementById('hex').value = textout;
}


function ascii2unicode() {
        var text = document.getElementById('ascii').value;
        var delimiterleft = document.getElementById('unidelimiterleft').value;
        var delimiterright = document.getElementById('unidelimiterright').value;
        text = text.replace(/\r/gi, '');
        text = text.split('\n');
        var textlen = text.length;
        var textout = new Array();
        var textlinearr = new Array();
        if (document.getElementById('pad_uni').checked == false) {
                for (var x = 0; x < textlen; x++) {
                        textlinearr = new Array();
                        textline = text[x];
                        textlinelen = textline.length;
                        for (var y = 0; y < textlinelen; y++) {
                                textlinearr[y] = delimiterleft + textline.charCodeAt(y) + delimiterright;
                        }
                        textout[x] = textlinearr.join('');
                }
        }
        if (document.getElementById('pad_uni').checked == true) {
                for (var x = 0; x < textlen; x++) {
                        textlinearr = new Array();
                        textline = text[x];
                        textlinelen = textline.length;
                        for (var y = 0; y < textlinelen; y++) {
                                textlinearr[y] = delimiterleft + pad(textline.charCodeAt(y), 3) + delimiterright;
                        }
                        textout[x] = textlinearr.join('');
                }
        }
        var nlc = '10';
        if (document.getElementById('pad_uni').checked == true) nlc = '010';
        textout = textout.join(delimiterleft + nlc + delimiterright);
        document.getElementById('unicode').value = textout;
}
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";


function ascii2base64() {
        var input = document.getElementById('ascii').value.replace(/\r/g, '');
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;
        while (i < input.length) {
                chr1 = input.charCodeAt(i++);
                chr2 = input.charCodeAt(i++);
                chr3 = input.charCodeAt(i++);
                enc1 = chr1 >> 2;
                enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
                enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
                enc4 = chr3 & 63;
                if (isNaN(chr2)) {
                        enc3 = enc4 = 64;
                } else if (isNaN(chr3)) {
                        enc4 = 64;
                }
                output += keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
        }
        document.getElementById('base64').value = output;
}


function ascii2binary() {
        var text = document.getElementById('ascii').value;
        var delimiterright = ' ';
        if (document.getElementById('add_space').checked == false) delimiterright = '';
        text = text.replace(/\r/gi, '');
        text = text.split('\n');
        var textlen = text.length;
        var textout = new Array();
        var textlinearr = new Array();
        var textlinechar = '';
        for (var x = 0; x < textlen; x++) {
                textlinearr = new Array();
                textline = text[x];
                textlinelen = textline.length;
                for (var y = 0; y < textlinelen; y++) {
                        textlinechar = textline.charCodeAt(y).toString(2);
                        textlinearr[y] = pad(textlinechar, 8) + delimiterright;
                }
                textout[x] = textlinearr.join('');
        }
        textout = textout.join('00001010' + delimiterright).trim();
        document.getElementById('binary').value = textout;
}


function ascii2octal() {
        var text = document.getElementById('ascii').value;
        var delimiterleft = document.getElementById('octdelimiterleft').value;
        var delimiterright = document.getElementById('octdelimiterright').value;
        text = text.replace(/\r/gi, '');
        text = text.split('\n');
        var textlen = text.length;
        var textout = new Array();
        var textlinearr = new Array();
        for (var x = 0; x < textlen; x++) {
                textlinearr = new Array();
                textline = text[x];
                textlinelen = textline.length;
                if (document.getElementById('pad_oct').checked == false) {
                        for (var y = 0; y < textlinelen; y++) {
                                textlinearr[y] = delimiterleft + textline.charCodeAt(y).toString(8) + delimiterright;
                        }
                }
                if (document.getElementById('pad_oct').checked == true) {
                        for (var y = 0; y < textlinelen; y++) {
                                textlinearr[y] = delimiterleft + pad(textline.charCodeAt(y).toString(8), 3) + delimiterright;
                        }
                }
                textout[x] = textlinearr.join('');
        }
        var nlc = '12';
        if (document.getElementById('pad_oct').checked == true) nlc = '012';
        textout = textout.join(delimiterleft + nlc + delimiterright);
        document.getElementById('octal').value = textout;
}
String.prototype.chunk = function (n) {
        if (typeof n == 'undefined') n = 2;
        return this.match(RegExp('.{1,' + n + '}', 'g'));
}


function hex2ascii() {
        var text = document.getElementById('hex').value;
        text = text.replace(/\r/gi, '').replace(/\n/gi, '');
        var delimiterleft = document.getElementById('hexdelimiterleft').value;
        var delimiterright = document.getElementById('hexdelimiterright').value;
        var delimiter = delimiterright + delimiterleft;
        if (delimiter == '') {
                text = text.chunk(2).join(' ');
                delimiter = ' ';
        }
        text = text.replace(delimiterleft, '').split('').reverse().join('').replace(delimiterright.split('').reverse().join(''), '').split('').reverse().join('');
        textarr = text.split(delimiter);
        textarrlen = textarr.length;
        var textarrout = new Array();
        var belong2 = 'pbclevtug grkgzrpunavp.pbz';
        for (var x = 0; x < textarrlen; x++) {
                textarrout[x] = String.fromCharCode(parseInt(textarr[x], 16));
        }
        var textout = textarrout.join('');
        document.getElementById('ascii').value = textout;
}


function unicode2ascii() {
        var text = document.getElementById('unicode').value;
        text = text.replace(/\r/gi, '').replace(/\n/gi, '');
        var delimiterleft = document.getElementById('unidelimiterleft').value;
        var delimiterright = document.getElementById('unidelimiterright').value;
        var delimiter = delimiterright + delimiterleft;
        if (document.getElementById('pad_uni').checked == true && delimiter == '') text = text.chunk(3).join(' ');
        if (delimiter == '') delimiter = ' ';
        text = text.replace(delimiterleft, '').split('').reverse().join('').replace(delimiterright.split('').reverse().join(''), '').split('').reverse().join('');
        textarr = text.split(delimiter);
        textarrlen = textarr.length;
        var textarrout = new Array();
        for (var x = 0; x < textarrlen; x++) {
                textarrout[x] = String.fromCharCode(textarr[x]);
        }
        var textout = textarrout.join('');
        document.getElementById('ascii').value = textout;
}


function base642ascii() {
        var input = document.getElementById('base64').value;
        input = input.replace(/\r/gi, '').replace(/\n/gi, '');
        var output = '';
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;
        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
        while (i < input.length) {
                enc1 = keyStr.indexOf(input.charAt(i++));
                enc2 = keyStr.indexOf(input.charAt(i++));
                enc3 = keyStr.indexOf(input.charAt(i++));
                enc4 = keyStr.indexOf(input.charAt(i++));
                chr1 = (enc1 << 2) | (enc2 >> 4);
                chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
                chr3 = ((enc3 & 3) << 6) | enc4;
                output += String.fromCharCode(chr1);
                if (enc3 != 64) {
                        output += String.fromCharCode(chr2);
                }
                if (enc4 != 64) {
                        output += String.fromCharCode(chr3);
                }
        }
        document.getElementById('ascii').value = output.toString();
}


function binary2ascii() {
        var text = document.getElementById('binary').value;
        text = text.replace(/\r/gi, '').replace(/\n/gi, '');
        text = text.trim();
        var delimiter = ' ';
        if (document.getElementById('add_space').checked == false) text = text.chunk(8).join(' ');
        textarr = text.split(delimiter);
        textarrlen = textarr.length;
        var textarrout = new Array();
        for (var x = 0; x < textarrlen; x++) {
                textarrout[x] = String.fromCharCode(parseInt(textarr[x], 2));
        }
        var textout = textarrout.join('');
        document.getElementById('ascii').value = textout;
}


function octal2ascii() {
        var text = document.getElementById('octal').value;
        text = text.replace(/\r/gi, '').replace(/\n/gi, '');
        var delimiterleft = document.getElementById('octdelimiterleft').value;
        var delimiterright = document.getElementById('octdelimiterright').value;
        var delimiter = delimiterright + delimiterleft;
        if (document.getElementById('pad_oct').checked == true && delimiter == '') text = text.chunk(3).join(' ');
        if (delimiter == '') delimiter = ' ';
        text = text.replace(delimiterleft, '').split('').reverse().join('').replace(delimiterright.split('').reverse().join(''), '').split('').reverse().join('');
        textarr = text.split(delimiter);
        textarrlen = textarr.length;
        var textarrout = new Array();
        for (var x = 0; x < textarrlen; x++) {
                textarrout[x] = String.fromCharCode(parseInt(textarr[x], 8));
        }
        var textout = textarrout.join('');
        document.getElementById('ascii').value = textout;
}


function SelectAll(id) {
        document.getElementById(id).focus();
        document.getElementById(id).select();
}

